/*
 * @Author: CPS
 * @Date: 2020-08-05 20:33:54
 * @Last Modified by: CPS
 * @Last Modified time: 2020-08-29 23:59:43
 * @Projectname Electron-fileWatcher
 * @file_path D:\CPS\MyProject\Electron-fileWatcher\src\renderer\pages\fileWatcher\js\fileList\fileListEvents.js
 * @Filename fileListEvents.js
 * fileList的点击事件模块
 */
'use strict'
import {
  openFile,
  openFolder
} from '@api/cmd.js'

//载入路由
import Router from '@api/router'

/**
 * 通过遍历,往上层查找对应clasname的父元素
 *
 * @param      {元素标签}  Element   The element
 * @param      {<string>}  findName  The find name
 * @param      {递归次数}  T         { parameter_description }
 * @return     {<type>}  The parent by class name.
 */
function getParentByClassName(Element, findName, T = 5) {
  if (T <= 0) return
  // log('开始查找:' + findName)
  let parent = Element.parentElement
  if (parent) {
    if (parent.className == findName) {
      return parent
    }
    T = T - 1
    return getParentByClassName(parent, findName, T)
  }
}


/*添加前缀函数*/
function prefix(name) {
  return 'fileList__' + name
}


let fileListEvents = {
  // 点击头部 触发折叠展现效果
  fileList__floderHeader: (tar) => {
    // 出发父元素的第二个子元素
    let parent = tar.parentNode
    let content = parent.getElementsByClassName(prefix('floderContent'))[0]
    let style //存放新的样式

    if (!content) return;
    if (content.offsetHeight > 0) {
      style = {
        padding: 0,
        height: 0,
        overflow: 'hidden',
        border: '0px solid #FFFFFF',
      };
    } else {
      // 计算高度，暂定 子元素个数 x 30px
      style = {
        padding: '5px',
        height: content.childElementCount * 27 + 'px',
        overflow: 'auto',
        border: '2px solid #42b983',
      }
    }

    //更新样式到dom
    Object.assign(content.style, style);
  },

  // 点击内部文件
  fileList__eachFileName: (tar, folderObject) => {
    //获取本节点的文件也
    let filename = tar.innerHTML
    let ext = filename.split('.').slice(-1).toString()

    //查找父级 eachFloder 元素
    let parent = getParentByClassName(tar, prefix('eachFloder'))

    // 查找子级 eachFloderName 元素 获取文件夹名称
    let foldername = parent.getElementsByClassName(prefix('eachFloderName'))[0].innerHTML

    //获取要打开的绝对路径
    if (!folderObject['folders'].hasOwnProperty(foldername)) return console.log('没有发现该目录信息')
    let fullname = folderObject['folders'][foldername]['fullPath'] + '/' + filename

    Router.post('/open', fullname)

    //调用系统相应程序打开文件(都是绝对路径)
    //openFile(fw['config']['exec'][ext], folderObject['folders'][foldername]['fullPath'] + '/' + filename)
  },


  // 打开选中的文件夹
  // 需要点击的对象 携带 info对象 （通过 addStructure.js 添加到html中)
  fileList__btnSelOpenFloder: (tar, folderObject) => {
    //查找父级 eachFloder 元素
    let parent = getParentByClassName(tar, prefix('eachFloder'))

    // 查找子级 eachFloderName 元素 获取文件夹名称
    let pathName = parent.getElementsByClassName(prefix('eachFloderName'))[0].innerHTML

    //通过folderObject 内相应的信息打开文件夹
    openFolder(folderObject['folders'][pathName]['fullPath'])
  },

  // 取消关联选中的文件夹
  fileList__btnCloseFloder: (tar) => {
    // log('关闭文件夹按钮被点击了')
    let parent = getParentByClassName(tar, prefix('eachFloder'))

    // 通过设置透明度获取动画效果
    parent.style.opacity = 0
    setTimeout(() => {
      parent.parentNode.removeChild(parent)
    }, 500, parent)
  }
}

// 传递要添加事件的元素标签
export default (fileList) => {
  //事件委托
  fileList.el.addEventListener('click', (e) => {
    console.log(e.target.className)

    // 通过判断classname进行不同部位的事件触发
    // 通过classname将对应的回调处理包装成对象
    if (fileListEvents.hasOwnProperty(e.target.className)) {
      fileListEvents[e.target.className](e.target, fileList['folderObject'])
    }
  }, true)
}